<template>
  <d2-container>
    <div class="mod-role">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList(true)" size="small">
        <!-- 添加接口名称输入框 -->
        <el-form-item label="接口名称">
          <el-input v-model="dataForm.interfaceName" placeholder="请输入接口名称"></el-input>
        </el-form-item>
        <!-- 根据需要添加搜索框 -->
        <el-form-item>
          <el-button @click="getDataList(true)">查询</el-button>
          <el-button v-if="isAuth('sys:device:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
          <el-button v-if="isAuth('sys:device:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;">
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column
          type="index"
          header-align="center"
          align="center"
          label="序号">
        </el-table-column>
        <el-table-column
          prop="interfaceName"
          header-align="center"
          align="center"
          label="接口名称">
        </el-table-column>
        <el-table-column
          prop="pushAddress"
          header-align="center"
          align="center"
          label="推送地址">
        </el-table-column>
        <el-table-column
          prop="portNumber"
          header-align="center"
          align="center"
          label="端口号">
        </el-table-column>
        <el-table-column
          prop="simCardStatus"
          header-align="center"
          align="center"
          label="SIM卡状态">
        </el-table-column>
        <el-table-column
          prop="signalStrength"
          header-align="center"
          align="center"
          label="信号强度">
        </el-table-column>
        <el-table-column
          prop="deviceName"
          header-align="center"
          align="center"
          label="设备名称">
        </el-table-column>
        <el-table-column
          prop="realTimeValue"
          header-align="center"
          align="center"
          label="实时值">
        </el-table-column>
        <el-table-column
          prop="usageAmount"
          header-align="center"
          align="center"
          label="用量">
        </el-table-column>
        <el-table-column
          prop="isControlled"
          header-align="center"
          align="center"
          label="是否控制">
        </el-table-column>
        <el-table-column
          prop="isActivated"
          header-align="center"
          align="center"
          label="是否开通">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="120"> <!-- 修改宽度为 120 -->
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.roleId)">修改</el-button>
            <el-button type="text" size="small" @click="deleteHandle(scope.row.roleId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="mt-4"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </d2-container>
</template>

<script>
  import AddOrUpdate from './role-add-or-update'
  export default {
    name: 'sys_device',
    data() {
      return {
        // 初始化接口名称字段
        dataForm: { interfaceName: '' },
        dataList: [
          { interfaceName: '接口1', pushAddress: 'http://example1.com/push1', portNumber: 8080, simCardStatus: '正常', signalStrength: '强', deviceName: '设备1', realTimeValue: '100', usageAmount: '50GB', isControlled: '是', isActivated: '是' },
          { interfaceName: '接口2', pushAddress: 'http://example2.com/push2', portNumber: 8081, simCardStatus: '异常', signalStrength: '弱', deviceName: '设备2', realTimeValue: '200', usageAmount: '30GB', isControlled: '否', isActivated: '否' },
          { interfaceName: '接口3', pushAddress: 'http://example3.com/push3', portNumber: 8082, simCardStatus: '正常', signalStrength: '中', deviceName: '设备3', realTimeValue: '150', usageAmount: '40GB', isControlled: '是', isActivated: '是' },
          { interfaceName: '接口4', pushAddress: 'http://example4.com/push4', portNumber: 8083, simCardStatus: '异常', signalStrength: '强', deviceName: '设备4', realTimeValue: '300', usageAmount: '20GB', isControlled: '否', isActivated: '否' },
          { interfaceName: '接口5', pushAddress: 'http://example5.com/push5', portNumber: 8084, simCardStatus: '正常', signalStrength: '弱', deviceName: '设备5', realTimeValue: '250', usageAmount: '60GB', isControlled: '是', isActivated: '是' },
          { interfaceName: '接口6', pushAddress: 'http://example6.com/push6', portNumber: 8085, simCardStatus: '异常', signalStrength: '中', deviceName: '设备6', realTimeValue: '350', usageAmount: '10GB', isControlled: '否', isActivated: '否' },
          { interfaceName: '接口7', pushAddress: 'http://example7.com/push7', portNumber: 8086, simCardStatus: '正常', signalStrength: '强', deviceName: '设备7', realTimeValue: '400', usageAmount: '70GB', isControlled: '是', isActivated: '是' },
          { interfaceName: '接口8', pushAddress: 'http://example8.com/push8', portNumber: 8087, simCardStatus: '异常', signalStrength: '弱', deviceName: '设备8', realTimeValue: '450', usageAmount: '80GB', isControlled: '否', isActivated: '否' },
          { interfaceName: '接口9', pushAddress: 'http://example9.com/push9', portNumber: 8088, simCardStatus: '正常', signalStrength: '中', deviceName: '设备9', realTimeValue: '500', usageAmount: '90GB', isControlled: '是', isActivated: '是' },
          { interfaceName: '接口10', pushAddress: 'http://example10.com/push10', portNumber: 8089, simCardStatus: '异常', signalStrength: '强', deviceName: '设备10', realTimeValue: '550', usageAmount: '100GB', isControlled: '否', isActivated: '否' }
        ],
        dataListLoading: false,
        dataListSelections: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 100,
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    methods: {
      getDataList(reset) {
        if (reset) {
          this.pageIndex = 1
        }
        // 现在可以使用 dataForm.interfaceName 作为查询条件
        // 这里可以添加获取数据的接口调用，将 dataForm.interfaceName 作为参数传递
      },
      sizeChangeHandle(newSize) {
        this.pageSize = newSize
        this.getDataList()
      },
      currentChangeHandle(newPage) {
        this.pageIndex = newPage
        this.getDataList()
      },
      selectionChangeHandle(selections) {
        this.dataListSelections = selections
      },
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      deleteHandle(id) {
        if (id) {
          // 这里可以添加删除单条数据的接口调用
        } else {
          // 这里可以添加批量删除数据的接口调用
        }
      },
      isAuth(permission) {
        // 这里可以添加权限验证逻辑
        return true
      }
    }
  }
</script>
